#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;

const int N = 2e5 + 10, M = 20;
int n, a[N], nxt[N][M], f[1 << M], ans, cnt;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i]--;
    }
    for (int o = 0; o < M; o++) {
        nxt[n][o] = nxt[n + 1][o] = n + 1;
    }
    for (int i = n; i >= 1; i--) {
        for (int o = 0; o < M; o++) {
            nxt[i - 1][o] = nxt[i][o];
        }
        nxt[i - 1][a[i]] = i;
    }
    f[0] = 0;
    for (int i = 1; i < (1 << M); i++) {
        f[i] = n + 1;
        cnt = 0;
        for (int o = 0; o < M; o++) {
            if (i & (1 << o)) {
                cnt++;
                f[i] = min(f[i], nxt[nxt[f[i ^ (1 << o)]][o]][o]);
            }
        }
        if (f[i] <= n) {
            ans = max(ans, cnt);
        }
    }
    cout << ans * 2 << endl;
    return 0;
}